function Validation(){
	var that = this;
	var form = [];
	form.email = {regexp: /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-z]{2,7}$/i, tooltip: "Enter a correct e-mail!", correct: false};
	form.firstName = {regexp: /^[a-zåäö]+(\-||\ )?[a-zåäö]*$/i, tooltip: "Enter name in letters", correct: false};
	form.lastName = {regexp: /^[a-zåäö]+(\-||\ )?[a-zåäö]*$/i, tooltip: "Enter name in letters", correct: false};
	form.dateOfBirth = {regexp: /^[0-9]{6}-[0-9]{4}$/, tooltip: "YYMMDD-XXXX", correct: false};
	form.password = {regexp: /^([\S]){8,}$/i, tooltip: "Enter at least 8 character", correct: false};
	form.secPassword = {regexp: /^([\S]){8,}$/i, tooltip: "Enter password again", correct: false};
	
	var input = document.getElementsByTagName("input");
	
	//Handles formparts
	for (var x=0; x<input.length; x++) {
		if (input[x].type === "text"){
			this.handleInput(input[x], form);
		}
	}
	
	this.clearForm(input);
}
//Clear textInputs
Validation.prototype.clearForm = function(input, select) {
	for (var i=0; i<input.length; i++) {
		if (input[i].type === "text"){
			input[i].value = "";
		}
	}
};
Validation.prototype.handleInput = function(input, form){
	var tooltip = document.createElement("div");
	input.onfocus = function(){
		var position = Validation.prototype.findPos(input);
		tooltip.id = "tooltip";
		tooltip.style.position = "absolute";
		tooltip.style.left = (position[0]+ 180)+"px";
		tooltip.style.top = (position[1])+"px";
		tooltip.innerHTML = form[input.id].tooltip;
		document.getElementById("registerBox").appendChild(tooltip);
	};
	input.onchange = function(){
		form[input.id].correct = (Validation.prototype.checkInput(form[input.id].regexp, this));
	};
	input.onblur = function(){
		document.getElementById("registerBox").removeChild(tooltip);
	};
};
Validation.prototype.findPos = function(input){
	var curleft = 0;
	var curtop = 0;
	if (input.offsetParent) {
		do {
			curleft += input.offsetLeft;
			curtop += input.offsetTop;
		}
		while (input === input.offsetParent);
		return [curleft, curtop];
	}
};
//Checking textinput with regExp
Validation.prototype.checkInput = function(regExp, input){
	if(regExp.test(input.value)){
		input.className = "textBox";
		return input.value;
	}
	else{
		input.className = "textBox error";
		return false;
	}
};
//Checking form
Validation.prototype.checkForm = function(input, form){
	for (var i = 0; i < input.length; i++) {
		if (input[i].type === "text") {
			if (form[input[i].id].correct === false) {
				return false;
			}
		}
	}
	return true;
};

window.onload = function(){
	new Validation();
	};
